package com.ppblock.shirox.conf;

import com.ppblock.shirox.core.AuthenticationInfo;
import com.ppblock.shirox.core.AuthenticationToken;
import com.ppblock.shirox.core.AuthorizationInfo;
import com.ppblock.shirox.core.AuthorizationRealm;
import com.ppblock.shirox.core.impl.SimpleAuthenticationInfo;
import com.ppblock.shirox.core.impl.SimpleAuthorizationInfo;
import com.ppblock.shirox.core.impl.UsernamePasswordToken;
import com.ppblock.shirox.model.Permission;
import com.ppblock.shirox.model.ShiroxRole;
import com.ppblock.shirox.model.User;

/**
 * @author yangjian
 * @since 17-12-6.
 */
public class ShiroRealm implements AuthorizationRealm {

	protected String name = "testRealm";

	@Override
	public String getName() {
		return name;
	}

	@Override
	public AuthorizationInfo doGetAuthorizationInfo(AuthenticationInfo authenticationInfo) {

		//这里需要通过身份认证信息获取到登录用户，然后再获取角色，权限信息
		User userInfo = (User) authenticationInfo.getUserInfo();
		//userInfo.getId();
		AuthorizationInfo info = new SimpleAuthorizationInfo();
		//添加测试角色数据
		info.addRole(new ShiroxRole(1,"admin"));
		info.addRole(new ShiroxRole(2, "manager"));
		info.addRole(new ShiroxRole(3, "employee"));

		//添加测试权限数据
		info.addPermission(new Permission("user:index"));
		info.addPermission(new Permission("user:add"));
		info.addPermission(new Permission("user:update"));
		info.addPermission(new Permission("user:delete"));

		return info;
	}

	@Override
	public AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) {
		UsernamePasswordToken passwordToken = (UsernamePasswordToken) token;
		Integer userId = 1;
		User user = new User(userId, passwordToken.getUsername(), passwordToken.getPassword(), "xxx0000@qq.com");
		return new SimpleAuthenticationInfo(getName(), user);
	}


}
